var core = require('../services/core');

function validateParam(req) {
    return !!(req.param('text') && req.param('text').length != 0 && req.param('scale') && req.param('scale').trim().length != 0);
}

exports.search = function (req, res) {
    res.json({errCode: 0, result: core.query(req.query)});
};

exports.searchPost = function (req, res) {
    res.json({errCode: 0, result: core.query(req.body)});
};

exports.filter = function (req, res) {
    if (!validateParam(req)) {
        res.json({errCode: 1, errMsg: '请求参数有误'});
        return;
    }
    var result = core.filterScaleByText(req.param('text'), req.param('scale'));
    res.json({errCode: 0, outScale: result});
};

exports.phonetic = function (req, res) {
    if (!req.param('text') || req.param('text').length == 0) {
        res.json({errCode: 1, errMsg: '请求参数有误'});
        return;
    }
    var result = core.phonetic(req.param('text'));
    res.json({errCode: 0, result: result});
};
